package com.feri.rbacsys.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feri.rbacsys.dao.DepositoryDao;
import com.feri.rbacsys.dto.OrderDetailDto;
import com.feri.rbacsys.dto.OrderDto;
import com.feri.rbacsys.entity.Order;
import com.feri.rbacsys.mapper.CheckMapper;
import com.feri.rbacsys.service.intf.OrderService;
import com.feri.rbacsys.mapper.OrderMapper;
import com.feri.rbacsys.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
* @author 24122
* @description 针对表【order】的数据库操作Service实现
* @createDate 2023-03-27 19:36:29
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{
@Resource
OrderMapper mapper;
@Resource
    CheckMapper checkMapper;
@Resource
    DepositoryDao depositoryDao;
    @Override
    public R getAllDto() {
        List<OrderDto> dtos = mapper.selectDtos();
        return R.ok(dtos);
    }

    @Override
    public R getById(Integer oid) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("id",oid);
        Order order = mapper.selectOne(wrapper);
        return R.ok(order);
    }



    @Override
    public R getOrderDetailDto(Integer oid) {
        OrderDetailDto detailDto = mapper.selectOrderDetailDto(oid);
        return R.ok(detailDto);
    }

    @Override
    public void importFile(MultipartFile file) throws IOException {
        if(file!=null){
            EasyExcel.read(file.getInputStream(), Order.class, new ReadListener<Order>() {
                @Override
                public void invoke(Order o, AnalysisContext analysisContext) {
                    o.init();
                    mapper.insert(o);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                }
            }).sheet().doRead();
        }
    }
}




